package com.ifcar.app.fragment;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.ifcar.app.activity.BaseActivity;
import com.ifcar.app.activity.MyApplication;
import com.ifcar.app.util.ThemeSettingsHelper;
import com.ifcar.app.view.MenuPopup;
import com.ifcar.app.view.MenuPopup.MenuCallback;
import com.ifcar.app.activity.R;

/**
 * 新闻 Fragment 基础类
 * 
 * @author wjying
 */
public class NRFragment extends BaseFragment implements MenuCallback {

	private MenuPopup mMenu;

	private boolean mIsFirst = true;

    // true表示当前显示, false表示当前不显示
    private boolean mInTabSelected = false;
	
	private View mTitleView;
	
	NRFragment mParent;

	@Override
	public final void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		if (hasTitleBar()) {
		    mTitleView = view.findViewById(R.id.top_main_bottom);
		    
		    if (mTitleView != null) {/*
		        mTitleView.setOnClickListener(new OnClickListener() {
	                @Override
	                public void onClick(View v) {
	                    dispatchActivityViewClick(v);
	                }
	            });
		    */}
		    
		}
		onViewCreatedNR(view, savedInstanceState);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		if (!((BaseActivity) getActivity()).isPaused()) {
			applyTheme();
		}
	}

	/**
	 * 在需要覆盖onViewCreated的时候,覆盖本方法
	 * 
	 * @param view
	 * @param savedInstanceState
	 */
	public void onViewCreatedNR(View view, Bundle savedInstanceState) {

	}

	@Override
    public void onDestroy() {
        super.onDestroy();
        if (mMenu != null && mMenu.isShowing()) {
            mMenu.dismiss();
        }
    }

    public MyApplication getApplication() {
        if(!isAdded()) {
            return null;
        }
		return ((BaseActivity) getActivity()).app;
	}

	public SQLiteDatabase getDb() {
	    if(!isAdded()) {
	        return null;
	    }
		return ((BaseActivity) getActivity()).db;
	}

	public ThemeSettingsHelper getThemeSettingsHelper() {
	    if(!isAdded()) {
	        return null;
	    }
		return ((BaseActivity) getActivity()).getThemeSettingsHelper();
	}

	/**
	 * 显示顶部loadingBar
	 */
	public void showProgress() {
        if (hasTitleBar()) {
            if (mTitleView == null) {
                return;
            }
            View v = mTitleView.findViewById(R.id.top_progressbar);
            if (v != null) {
                v.setVisibility(View.VISIBLE);
            }
        } else {
            if (!mInTabSelected) {
                return;
            }
            if (!isAdded()) {
                return;
            }
            View v = getActivity().findViewById(R.id.top_progressbar);
            if (v != null) {
                v.setVisibility(View.VISIBLE);
            }
        }
	}

	/**
	 * 隐藏顶部loadingBar
	 */
	public void hideProgress() {
        if (hasTitleBar()) {
            if (mTitleView == null) {
                return;
            }
            View v = mTitleView.findViewById(R.id.top_progressbar);
            if (v != null) {
                v.setVisibility(View.GONE);
            }
        } else {
            if (!mInTabSelected) {
                return;
            }
            if (!isAdded()) {
                return;
            }
            View v = getActivity().findViewById(R.id.top_progressbar);
            if (v != null) {
                v.setVisibility(View.GONE);
            }
        }
	}

	/**
	 * 设置顶部title
	 * 
	 * @param title
	 */
	public void setTitle(int title) {
		setTitle(getString(title));
	}

	/**
	 * 设置顶部title
	 * 
	 * @param title
	 */
	public void setTitle(CharSequence title) {
        if (hasTitleBar()) {
            if (mTitleView == null) {
                return;
            }
            View v = mTitleView.findViewById(R.id.top_main_text);
            if (v != null && v instanceof TextView) {
                ((TextView) v).setText(title);
            }
        } else {
            if (!mInTabSelected) {
                return;
            }
            if (!isAdded()) {
                return;
            }
            View v = getActivity().findViewById(R.id.top_main_text);
            if (v != null && v instanceof TextView) {
                ((TextView) v).setText(title);
            }
        }
	}
	
    /**
     * activity是否带title bar
     */
    public boolean hasTitleBar() {
        return false;
    }
    
    public View getTitleView() {
        return mTitleView;
    }

	public boolean dispatchKeyEvent(KeyEvent event) {
		if (!mInTabSelected) {
			return false;
		}
		if (mMenu == null) {
			return false;
		}
		if(!isAdded()) {
            return false;
        }
		return mMenu.dispatchKeyEvent(event);
	}

	public View getMenuView() {
	    if(!isAdded()) {
            return null;
        }
		if (mMenu != null) {
			return mMenu.getContentView();
		}
		return null;
	}

	/**
	 * 设置菜单
	 * 
	 * @param menuLayoutId
	 *            菜单layout
	 * @param ids
	 *            菜单项view id
	 */
	public final void setNRMenu(int menuLayoutId, int[] ids) {
		mMenu = new MenuPopup(getActivity());
		mMenu.setMenuCallback(this);
		mMenu.setContentView(menuLayoutId);
		mMenu.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mMenu.dismiss();
				onMenuClick(v.getId());
			}
		}, ids);
	}

	/**
	 * 菜单点击回调
	 * 
	 * @param id
	 */
	public void onMenuClick(int id) {

	}

	public void applyTheme() {
	    if(!isAdded()) {
            return;
        }
		onApplyTheme();
	}

	/**
	 * 重写此方法,在主题切换时调用
	 */
	public void onApplyTheme() {

	}

	/**
	 * 当本fragment在一个tab中由选中变为不选中时,当选中时调用此方法，方便使用<br/>
	 * fragment不显示时调用<br/>
	 */
	public final void unSelectInTab() {
	    if (!mInTabSelected) {
	        return;
	    }
	    if(!isAdded()) {
            return;
        }
		onTabUnSelected();
	    mInTabSelected = false;
	}

	/**
	 * 当本fragment在一个tab中由选中变为不选中时会调用,可以在本方法中做一些需要回收的操作<br/>
	 */
	public void onTabUnSelected() {

	}

	/**
	 * 当本fragment在一个tab中时,当选中时调用此方法，方便使用<br/>
	 * fragment显示时调用<br/>
	 */
	public void selectInTab() {
	    if (mInTabSelected) {
	        return;
	    }
	    if(!isAdded()) {
            return;
        }
		mInTabSelected = true;
		onTabSelected(mIsFirst);
		if (mIsFirst) {
			mIsFirst = false;
		}
	}

	/**
	 * 当本fragment在一个tab中时,当选中时会调用<br/>
	 */
	public void onTabSelected(boolean first) {

	}

	/**
	 * 当调用selectInTab后为true,当调用unSelectInTab后为false
	 */
	public final boolean inTabSelected() {
		return mInTabSelected;
	}

	@Override
	public void onMenuClose() {
	}

	@Override
	public boolean onMenuOpen() {
		return true;
	}
	
    public boolean dispatchActivityViewClick(View view) {
        return onActivityViewClick(view);
    }

    public boolean onActivityViewClick(View view) {
        return false;
    }
    
    public NRFragment getParent() {
        return mParent;
    }
    
    /**
     * 当fragment绑定到CommonFrameActivity时，如果返回不为空时,
     * 会将本fragment先移除，再根据返回的Bundle作为fragment参数重新新建fragment
     * 
     * @return
     */
    public Bundle needRecreate() {
        return null;
    }
}
